<?php
/**
 * alltosun.com 播放单 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2015 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王磊 (wangl@alltosun.com) $
 * $Date: 2015-12-14 下午3:20:35 $
*/

class Action
{
    private $user_id;
    private $per_page = 10;

    public function __construct()
    {
        $this->user_id = user_helper::get_user_id();
    }

    /**
     * 播放单列表
     */
    public function index()
    {
        // 接收参数
        $status = Request::getParam('status', 1);
        $search_filter = Request::getParam('search_filter', array());
        $page_no = Request::getParam('page_no', 1);

        // 拼装搜索条件
        $filter = array();
        $filter['status'] = $status;
        // $filter['progress'] = 1;

        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['title LIKE'] = '%'.$search_filter['title'].'%';
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }

        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 59:59:59';
        }

        $count = _model('play_list')->getTotal($filter);
        $play_list = array();

        if ($count) {
            $pager = new Pager($this->per_page);
            $limit = $pager->getLimit($page_no);
            $order = 'ORDER BY `id` DESC ';

            if ($pager->generate($count)) {
                Response::assign('pager', $pager);
            }
            // 查询数据
            $play_list = _model('play_list')->getList($filter, $order.$limit);
        }

        // 分配变量
        Response::assign('play_list', $play_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);

        // 渲染模板
        Response::display('admin/index.html');
    }

    /**
     * 添加播放单
     */
    public function add()
    {
        Response::display('admin/add.html');
    }

    /**
     * 编辑播放单
     */
    public function edit()
    {
        // request
        $id = Request::getParam('id', 0);

        if (!$id) {
            return '请选择编辑内容';
        }
        $info = _uri('play_list', $id);

        if (!$info || $info['status'] == 0) {
            return '内容不存在或者已删除';
        }

        // assign
        Response::assign('info', $info);

        // display
        Response::display('admin/add.html');
    }

    /**
     * 保存播放单
     */
    public function save()
    {
        // request
        $info = Request::getParam('info', array());
        $id = Request::getParam('id', 0);

        // check
        if (!isset($info['title']) || !$info['title']) {
            return '请输入播放单名';
        }

        // 素材的播放地址
        $info['province'] = isset($info['province']) && $info['province'] ? $info['province'] : '';
        $info['city'] = isset($info['city']) && $info['city'] ? $info['city'] : '';
        $info['is_nationwide'] = 1;

        if ($info['province'] && $info['city']) {
            $info['is_nationwide'] = 0;
        }

        if (!isset($info['start_time']) || !$info['start_time']) {
            return '请选择开始时间';
        }
        if (!isset($info['stop_time']) || !$info['stop_time']) {
            return '请选择结束时间';
        }

        if (!strtotime($info['start_time']) || !$info['stop_time']) {
            return '请输入正确时间格式';
        }

        $info['start_time'] .= ' 00:00:00';
        $info['stop_time'] .= ' 23:59:59';
        // an_dump($info);exit();
        if ($id) {
            _model('play_list')->update($id, $info);
        } else {
            $info['op_id'] = $this->user_id;
            $id = _model('play_list')->create($info);
            if (!$id) {
                return '添加失败';
            }
        }
        Response::redirect('play_list/admin');
    }

    /**
     * 删除播放单
     * @return string|multitype:string
     */
    public function delete()
    {
        $ids = Request::getParam('id');

        if (!$ids) {
            return '请选择要删除的内容';
        }
        $id = explode(',', trim($ids));

        foreach ($id as $v) {
            $info = _uri('play_list', $v);
            if (!$info) {
                continue;
            }
            if ($info['status']) {
                _model('play_list')->update($v, array('status'=>0));
            } else {
                //_model('material')->delete($v);
            }
        }
        return array('info'=>'ok');
    }

    /**
     * 还原播放单
     */
    public function change_status()
    {
        $id = Request::getParam('id', 0);

        if (!$id) {
            return '请选择要操作的内容';
        }

        $info = _uri('play_list', $id);
        if (!$info || $info['status']) {
            return '该内容不存在，或以执行过相同操作';
        }

        _model('play_list')->update($id, array('status'=>1));

        return array('info'=>'ok');
    }

    /**
     * 添加素材
     */
    public function add_material()
    {
        // 接收参数
        $id = Request::getParam('id', 0);
        $search_info = Request::getParam('search_info', array());
        $page_no = Request::getParam('page_no', 1);

        if (!$id) {
            return '请选择播放单';
        }

        // 播放单信息
        $info = _uri('play_list', $id);

        if (!$info || $info['status'] == 0) {
            return '该内容不存在或者已删除';
        }

        // 播放单中的素材
        $play_material_list = material_helper::get_play_list_material($id);

        // 播放单中素材的id
        $play_material_ids = material_helper::get_play_material_ids($id);

        // 查询所有素材
        $filter = array('progress'=>2, 'status'=>1);
        if ($search_info) {
            if ($search_info['province'] == '全国') {
                $filter['is_nationwide'] = 1;
                $search_info['city'] = '全国';
            } else if ($search_info['province'] == '全部') {
                $search_info['province'] = '';
                $search_info['city'] = '全部';
            } else {
                $filter['province'] = $search_info['province'];
                $filter['city'] = $search_info['city'];
            }
        }

        $count = _model('material')->getTotal($filter);
        $material_list = array();

        if ($count) {
            $pager = new Pager($this->per_page);
            $limit = $pager->getLimit($page_no);

            if ($pager->generate($count)) {
                Response::assign('pager', $pager);
            }
            $material_list = material_helper::get_material_list($filter, $limit);   // 素材列表
        }

        // assign 
        Response::assign('info', $info);
        Response::assign('play_material_list', $play_material_list);
        Response::assign('play_material_ids', $play_material_ids);
        
        Response::assign('material_list', $material_list);
        Response::assign('search_info', $search_info);

        // display
        Response::display('admin/add_material.html');
    }

    public function do_add_material()
    {
        $material_id = trim(Request::getParam('id'));
        $play_list_id = Request::getParam('play-list-id', 0);
        $is_delete = Request::getParam('is_delete', 0);

        // check
        if (!$material_id) {
            return '请选择素材';
        }
        if (!$play_list_id) {
            return '请选择播放单';
        }

        $play_list_info = _uri('play_list', $play_list_id);

        if (!$play_list_info || $play_list_info['status'] == 0) {
            return '播放单不存在或者已删除';
        }

        $filter = array('play_list_id'=>$play_list_id);

        // 移除播放单走的流程
        if ($is_delete) {
            $filter['material_id'] = $material_id;
            _model('play_material')->delete($filter);
            return array('info'=>'ok');
        }

        // 添加播放单的流程
        $material_ids = explode(',', $material_id);

        foreach ($material_ids as $v) {
            if (play_list_helper::material_is_in_play_list($v, $play_list_id)) {
                continue;
            }
            $filter['material_id'] = $v;
            _model('play_material')->create($filter);
        }
        return array('info'=>'ok');
    }

    public function save_sort()
    {
        $play_id = Request::getParam('play_id', 0);
        $sort    = Request::getParam('sort', '');

        if (!$play_id || !$sort) {
            return '保存失败';
        }

        $material_ids = explode(',', $sort);

        $num = count($material_ids);

        foreach ($material_ids as $k=>$v) {
            $filter = array('play_list_id'=>$play_id, 'material_id'=>$v);
            $update_filter = array('sort'=>$num--);
            _model('play_material')->update($filter, $update_filter);
        }

        return array('info'=>'ok');
    }
}
?>